package xm.components.supportClasses.base
{
	import xm.align.AlignFactory;
	import xm.control.Navigator;
	import xm.control.bar.barContent.ActionContent;
	import xm.control.bar.barContent.NavigationContent;
	import xm.control.bar.barContent.TabContent;
	import xm.control.bar.barContent.TitleContent;
	import xm.core.XMSprite;
	import xm.definition.Arrange;
	import xm.interfaces.ITransition;
	
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class ComponentBase extends XMSprite implements ITransition
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		/**
		 * 정렬값  Arrange.VERTICAL, Arrange.HORIZONTAL
		 */		
		public var arrangement : String = Arrange.VERTICAL;
		
		/**
		 * 트랜지션 사용여부 
		 */		
		public var useTransition : Boolean = true;
		
		/**
		 * 화면관장하는 네비게이터 객체( 싱글턴 참조 ) 
		 */		
		public var navigator : Navigator;
		
		
		/**
		 * 자동정렬 - container 위치부터 사용가능.
		 */		
		public var isAutoAlign : Boolean = false;
		
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		/**
		 * 정렬객체 
		 */		
		protected var af : AlignFactory = new AlignFactory();
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		private var _navigationContent : NavigationContent;
		private var _titleContent : TitleContent;
		private var _actionContent : ActionContent;
		private var _tabContent : TabContent;
		
		private var _useTabContent : Boolean = true;
		private var _useNavigationContent : Boolean = true;
		private var _useTitleContent : Boolean = true;
		private var _useActionContent : Boolean = true;
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function ComponentBase()
		{
			super();
		}
		
		

		/**
		 * commitProperties
		 */		
		override public function commitProperties():void
		{
			super.commitProperties();
		}
		
		
		/**
		 * <br>ex )</br>
		 * <br>  - af.addSimpleItem( item, [0,0,0,0] ); // 패딩은 상,우,하,좌 순서임. 기본값은 null </br>
		 * <br>  - af.addSimpleBox( new AlignHBox().addSimpleItem( item, [0,0,0,0] ) );</br>
		 * <br>  - af 를 작동하기 위해서는 최후 af.workingAlign(); 를 호출해 주어야 한다.</br>
		 * updateDisplayList
		 */		
		override public function updateDisplayList( enforce : Boolean = false ):void
		{
			super.updateDisplayList(enforce);
			
			af.destroy();
			af.setAlignFactory( 0,null,arrangement,null );
			
			var len:int = numChildren;
			for (var i:int = 0; i < len; i++) 
			{
				if( getChildAt( i ).hasOwnProperty( "isAutoAlign" ) == true
					&& getChildAt( i )["isAutoAlign"] == true )
				{
					if( getChildAt( i ) is ComponentBase )
					{
						var comb : ComponentBase = getChildAt( i ) as ComponentBase;
						af.addSimpleItem( comb, comb.padding );
					}
					else
					{
						af.addSimpleItem( getChildAt( i ) );
					}
				}
			}
			
			af.workingAlign();
		}
		
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * createChildren
		 */		
		override public function createChildren():void
		{
			super.createChildren();
			navigator = Navigator.getInstance();
		}
		
		
		/**
		 * createComplete
		 */		
		override public function creationComplete():void
		{
			super.creationComplete();
			
		}
		
		
		
		/**
		 * startAddTransition : add 완료 후 transition 이 있으면 화면전환효과를 실행한다.
		 */		
		public function startAddTransition():void
		{
			navigator.navigationContent = ( useNavigationContent ) ? navigationContent : null;
			navigator.titleContent = ( useTitleContent ) ? titleContent : null;
			navigator.actionContent = ( useActionContent ) ? actionContent : null;
			navigator.tabContent = ( useTabContent ) ? tabContent : null;
			
			
			this.invalidateDisplayList();
		}
		
		/**
		 * startAddTransition 완료.
		 */		
		public function endAddTransition():void
		{
			
		}
		
		
		/**
		 * startRemoveTransition : remove 하기 전 transition 이 있으면 화면전환효과를 실행한다.
		 */		
		public function startRemoveTransition():void
		{
			
		}
		
		
		/**
		 * startRemoveTransition 완료.
		 */		
		public function endRemoveTransition():void
		{
			
		}
		
		
		/**
		 * destroy
		 */		
		override public function destroy():void
		{
			
			arrangement = null;
			useTransition = false;
			navigator = null;
			isAutoAlign = false;
			
			if( af )
			{
				af.destroy();
				af = null;
			}
			
			if( _navigationContent ) _navigationContent.destroy();
			_navigationContent = null;
			
			if( _titleContent ) _titleContent.destroy();
			_titleContent = null;
			
			if( _actionContent ) _actionContent.destroy();
			_actionContent = null;
			
			if( _tabContent ) _tabContent.destroy();
			_tabContent = null;
			
			_useTabContent = false;
			_useNavigationContent = false;
			_useTitleContent = false;
			_useActionContent = false;
			
			super.destroy();
			
		}
		
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		public function get useTabContent():Boolean
		{
			return _useTabContent;
		}
		
		public function set useTabContent(value:Boolean):void
		{
			_useTabContent = value;
		}
		
		public function get useActionContent():Boolean
		{
			return _useActionContent;
		}
		
		public function set useActionContent(value:Boolean):void
		{
			_useActionContent = value;
		}
		
		public function get useTitleContent():Boolean
		{
			return _useTitleContent;
		}
		
		public function set useTitleContent(value:Boolean):void
		{
			_useTitleContent = value;
		}
		
		public function get useNavigationContent():Boolean
		{
			return _useNavigationContent;
		}
		
		public function set useNavigationContent(value:Boolean):void
		{
			_useNavigationContent = value;
		}

		
		/**
		 * @return : TabContent
		 */		
		public function get tabContent():TabContent
		{
			return _tabContent;
		}
		
		/**
		 * @param value : 화면 하단의 TabContent - TabButton 이 삽입된 content
		 */		
		public function set tabContent(value:TabContent):void
		{
			_tabContent = value;
		}

		
		/**
		 * @return : ActionContent
		 */		
		public function get actionContent():ActionContent
		{
			return _actionContent;
		}
		
		/**
		 * @param value : 화면 우상단 action content
		 */		
		public function set actionContent(value:ActionContent):void
		{
			_actionContent = value;
		}
		
		/**
		 * @return : TitleContent
		 */		
		public function get titleContent():TitleContent
		{
			return _titleContent;
		}
		
		/**
		 * @param value : 화면 상단 타이틀
		 */		
		public function set titleContent(value:TitleContent):void
		{
			_titleContent = value;
		}
		
		/**
		 * @return : NavigationContent
		 */		
		public function get navigationContent():NavigationContent
		{
			return _navigationContent;
		}
		
		/**
		 * @param value : 화면 좌상단 네비게이션
		 */		
		public function set navigationContent(value:NavigationContent):void
		{
			_navigationContent = value;
		}
		
		
		
		
		
		
		/**
		 * 실제 사용가능한 너비.
		 */		
		public function get availableWidth() : Number
		{
			return navigator.getAvailablePageWidth();
		}
		
		/**
		 * 실제 사용가능한 높이.
		 */		
		public function get availableHeight() : Number
		{
			return navigator.getAvailablePageHeight();
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		
		
		
	}
}